//
//  NSConversitionNavView.m
//  Meboo
//
//  Created by kyo on 2023/10/16.
//

#import "NSConversitionNavView.h"
#import "NSModelDetailViewController.h"
@interface NSConversitionNavView ()
@property (nonatomic, strong) UIButton *leftBtn;
@property (nonatomic, strong) UIButton *moreBtn;
@property (nonatomic, strong) UILabel *titleLab;
@property (nonatomic, strong) NSMutableArray *reasonArr;
@end

@implementation NSConversitionNavView

#pragma mark - Life Cycle
- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self setupUI];
    }
    return self;
}

- (void)setUserId:(NSString *)userId {
    _userId = userId;
    if ([userId isEqualToString:[MBUserAccount defaultAccount].kefuTargetId]) {
        self.moreBtn.hidden = YES;
    } else {
        self.moreBtn.hidden = NO;
    }
}

- (void)setNameStr:(NSString *)nameStr {
    _nameStr = nameStr;
    
    if ([self.userId isEqualToString:[MBUserAccount defaultAccount].kefuTargetId]) {
        self.titleLab.text = [NSString stringWithFormat:@"%@",nameStr];
    } else {
        self.titleLab.text = [NSString stringWithFormat:@" %@",nameStr];

        // 创建一个NSMutableAttributedString来存储多行文字和图片
        NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc] initWithString:self.titleLab.text];

        // 添加图片
        NSTextAttachment *attachment = [[NSTextAttachment alloc] init];
        attachment.image = ImageWithName(@"home_list_vip_icon");
        attachment.bounds = CGRectMake(0, 0, MBHeightScale(20), MBHeightScale(20)); // 设置图片大小
        NSAttributedString *attachmentString = [NSAttributedString attributedStringWithAttachment:attachment];
        
        
        NSMutableAttributedString *newAtt = [[NSMutableAttributedString alloc] initWithAttributedString:attachmentString];
        
        [newAtt appendAttributedString:attributedText];
        self.titleLab.attributedText = newAtt;
    }
}

#pragma mark - Action

- (void)moreAction {
    __WeakSelf
        //获取
        UIAlertController  *alert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    if ([self getIsIpad]){
        alert.popoverPresentationController.sourceView = self;
        alert.popoverPresentationController.sourceRect = CGRectMake(ScreenWidth/2,ScreenHeight/2,0,0);
        alert.popoverPresentationController.permittedArrowDirections = 0;
    }
        
        UIAlertAction  *reportAction = [UIAlertAction actionWithTitle:@"View Profile" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//            [weakSelf selectReasonAction];
            NSModelDetailViewController *modelDetailVc = [NSModelDetailViewController new];
            modelDetailVc.modelStyle = ChatModelDetail;
            modelDetailVc.userId = self.userId;
            [weakSelf.currentViewController.navigationController pushViewController:modelDetailVc animated:YES];
        }];
        
        UIAlertAction *BlockAction = [UIAlertAction actionWithTitle:@"Delete" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            [[MBChatDataManager sharedManager] reportBlockUserWithUserId:self.userId reason:@"" type:1 success:^(BOOL success) {
                if (success) {
                    [weakSelf deleteMessage:@"delete success"];
                }
            }];
        }];
        
        UIAlertAction  *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        }];
        
        [alert addAction:reportAction];
        [alert  addAction:BlockAction];
        [alert addAction:cancelAction];
        [self.currentViewController presentViewController:alert animated:YES completion:nil];
    
    /**
     埋点
     点击更多Chat_More
     */
    [[NSIOApiManager sharedManager] setIOWithEventName:@"Chat_More"];
}

- (void)selectReasonAction {
    __WeakSelf
    //获取
    UIAlertController  *alert = [UIAlertController alertControllerWithTitle:@"What went wrong？" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    if ([self getIsIpad]){
        alert.popoverPresentationController.sourceView = self;
        alert.popoverPresentationController.sourceRect = CGRectMake(ScreenWidth/2,ScreenHeight/2,0,0);
        alert.popoverPresentationController.permittedArrowDirections = 0;
    }
    for (NSString *title in self.reasonArr) {
        UIAlertAction  *action = [UIAlertAction actionWithTitle:title style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            [[MBChatDataManager sharedManager] reportBlockUserWithUserId:weakSelf.userId reason:title type:4 success:^(BOOL success) {
                if (success) {
                    [weakSelf deleteMessage:@"report success"];
                }
            }];
        }];
        [alert addAction:action];
    }
    UIAlertAction  *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
    }];
    [alert addAction:cancelAction];
    [self.currentViewController presentViewController:alert animated:YES completion:nil];
}

//如果想要判断设备是ipad，要用如下方法
- (BOOL)getIsIpad {
    NSString *deviceType = [UIDevice currentDevice].model;
    
    if([deviceType isEqualToString:@"iPhone"]) {
        //iPhone
        return NO;
    }
    else if([deviceType isEqualToString:@"iPod touch"]) {
        //iPod Touch
        return NO;
    }
    else if([deviceType isEqualToString:@"iPad"]) {
        //iPad
        return YES;
    }
    return NO;
}

- (void)deleteMessage:(NSString *)noticeStr {
    __WeakSelf
    [[RCCoreClient sharedCoreClient] removeConversation:ConversationType_PRIVATE targetId:weakSelf.userId completion:^(BOOL ret) {
        dispatch_async(dispatch_get_main_queue(), ^{
            [weakSelf.currentViewController.navigationController popViewControllerAnimated:YES];
            [MBProgressHUD showSuccessMessage:noticeStr];
        });
    }];
    /**
     埋点
     点击拉黑Chat_More_Block
     */
    [[NSIOApiManager sharedManager] setIOWithEventName:@"Chat_More_Block"];
}

- (void)backAction {
    [self.currentViewController.navigationController popViewControllerAnimated:YES];;
}

#pragma mark - UI
- (void)setupUI {
    
    self.backgroundColor = UIColor.clearColor;
    
    [self addSubview:self.moreBtn];
    [self.moreBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(MBWidthScale(-20));
        make.bottom.mas_equalTo(MBHeightScale(-5));
        make.width.height.mas_equalTo(MBHeightScale(30));
    }];
    
    [self addSubview:self.titleLab];
    [self.titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self);
        make.left.mas_equalTo(MBWidthScale(55));
        make.right.mas_equalTo(MBWidthScale(-55));
        make.bottom.mas_equalTo(MBHeightScale(-5));
    }];
    
    [self addSubview:self.leftBtn];
    [self.leftBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(MBWidthScale(20));
        make.centerY.equalTo(self.titleLab);
        make.width.height.mas_equalTo(MBHeightScale(20));
    }];
}

#pragma mark - Setter && Getter
- (UIButton *)moreBtn {
    if (!_moreBtn) {
        _moreBtn = [[UIButton alloc] init];
        [_moreBtn setYXEnlargeEdgeWithTop:10 right:10 bottom:10 left:10];
        [_moreBtn setImage:ImageWithName(@"chat_more") forState:0];
        [_moreBtn addTarget:self action:@selector(moreAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _moreBtn;
}

- (UIButton *)leftBtn {
    if (!_leftBtn) {
        _leftBtn = [[UIButton alloc] init];
        [_leftBtn setYXEnlargeEdgeWithTop:10 right:10 bottom:10 left:10];
        [_leftBtn setImage:ImageWithName(@"back_arrow_black") forState:0];
        [_leftBtn addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _leftBtn;
}

- (UILabel *)titleLab {
    if (!_titleLab) {
        _titleLab = [[UILabel alloc] init];
        _titleLab.textAlignment = NSTextAlignmentCenter;
        _titleLab.font = MBHelBoldFontSizeScale(20);
        _titleLab.textColor = HEXS(@"333333");
    }
    return _titleLab;
}

- (NSMutableArray *)reasonArr {
    if (!_reasonArr) {
        _reasonArr = [NSMutableArray arrayWithArray:@[@"Not my type",@"Fake profile! It’s a guy",@"Abusive behaviour or nudity",@"Scam",@"Sex services",@"Sale or mentions of drugs",@"Underage",]];
    }
    return _reasonArr;
}
@end
